package com.bignerdranch.android.furnitureapplication;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bignerdranch.android.furnitureapplication.adapter.FurnitureInfoAdapter;
import com.bignerdranch.android.furnitureapplication.entity.FurnitureInfoBean;
import com.bignerdranch.android.furnitureapplication.entity.RedWoodInfoBean;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ShowCollectionActivity extends AppCompatActivity {

    private String getUserAccount;
    private List<FurnitureInfoBean> mFurnitureInfoBeans;
    private RecyclerView mRecyclerView;
    private FurnitureInfoAdapter mFurnitureInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_collection);

        SharedPreferences preferences = getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "bbchond");
        getCollectionFurniture(getUserAccount);

        mRecyclerView = findViewById(R.id.collection_recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    private void getCollectionFurniture(String userAccount) {
        RetrofitHelper.getNewsService()
                .getFurnitureCollectionInfo(userAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<FurnitureInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        mFurnitureInfoAdapter = new FurnitureInfoAdapter(mFurnitureInfoBeans);
                        mRecyclerView.setAdapter(mFurnitureInfoAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<FurnitureInfoBean> furnitureInfoBeans) {
                        mFurnitureInfoBeans = furnitureInfoBeans;
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCollectionFurniture(getUserAccount);
    }
}
